import mysql.connector
from mysql.connector import Error

def init_database():
    try:
        # 连接MySQL服务器
        connection = mysql.connector.connect(
            host="localhost",
            user="root",
            password="your_password"
        )
        
        if connection.is_connected():
            cursor = connection.cursor()
            
            # 创建数据库
            cursor.execute("CREATE DATABASE IF NOT EXISTS qa_system")
            cursor.execute("USE qa_system")
            
            # 创建文件表
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS files (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    filename VARCHAR(255) NOT NULL,
                    file_type VARCHAR(100) NOT NULL,
                    file_path VARCHAR(255) NOT NULL,
                    upload_time DATETIME NOT NULL
                )
            """)
            
            # 创建问答记录表
            cursor.execute("""
                CREATE TABLE IF NOT EXISTS qa_records (
                    id INT AUTO_INCREMENT PRIMARY KEY,
                    question TEXT NOT NULL,
                    answer TEXT NOT NULL,
                    confidence FLOAT,
                    processing_time FLOAT,
                    created_at DATETIME NOT NULL
                )
            """)
            
            connection.commit()
            print("数据库初始化成功！")
            
    except Error as e:
        print(f"数据库初始化失败: {e}")
        
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()

if __name__ == "__main__":
    init_database() 